/*
package com.alibaba.iot.demo;

import com.alibaba.iot.demo.util.Base64Util;
import com.alibaba.iot.demo.util.Config;
import com.alibaba.iot.demo.util.IotAuthUtil;
import com.alibaba.iot.demo.util.Md5;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Map;

*/
/**
 * ������:
 * 16/4/12.
 *//*

public class MQTTClient {
	
    public static void main(String[] args) throws Exception {

        //Step 1: ��ȡ������Ϣ
    	
    	
        System.out.println("��ʼ��ȡ������Ϣ!");
        Map<String, String> result = IotAuthUtil.auth(Config.deviceName, Config.deviceSecret);
		
        //1.1 �õ���Կ��BASE64�����Ժ���ַ����
        String pubKey = result.get("pubkey");
		
        //1.2 �õ����ӵ�Ŀ�ĵ�IP��˿�
        String servers = result.get("servers");
        String targetServer = servers.substring(0, servers.indexOf("|"));

        //1.3 �õ�BASE64�ַ�����Ժ�Ĺ�Կ֤���ļ�
        byte[] pubKeyByteContent = Base64Util.decode(pubKey);
        
        //1.4 �õ��豸��ID
        result = IotAuthUtil.auth(Config.deviceName2, Config.deviceSecret2);
        String deviceId2 = result.get("deviceId");

        System.out.println("��ȡ���óɹ�! �����ַ: " + targetServer);

    
        //Step 3: �豸2���벢������Ϣ��Topic��
        deviceConnectAndPub(pubKeyByteContent, targetServer, deviceId2);
        
        

    }
    
    private static void deviceConnectAndPub(byte[] stest, String targetServer, String deviceId) throws Exception {
        System.out.println("==================================================");
        System.out.println("�豸:" + Config.deviceName2 + "��ʼ�������ӡ���");
        System.out.println("==================================================");
        final String topic = Config.topic;

        String broker = "ssl://" + targetServer;
        //�ͻ���ID��ʽ: productKey + deviceId.
        String clientId = Config.productKey + ":" + deviceId;

        MemoryPersistence persistence = new MemoryPersistence();
        SSLSocketFactory socketFactory = createSSLSocket(stest);
        try {
            final MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setMqttVersion(4);// MQTT 3.1.1
            connOpts.setSocketFactory(socketFactory);
            String signUserName = signUserName4Device2(deviceId);
            connOpts.setUserName(signUserName);
            connOpts.setKeepAliveInterval(65);
            System.out.println(Config.deviceName2 + "��������, Ŀ�ĵ�: " + broker);
            sampleClient.connect(connOpts);
            sampleClient.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    System.out.println("����ʧ��,ԭ��:" + cause);
                }

                @Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    System.out.println("受到 Topic [" + topic + "] , 消息:[" + new String(message.getPayload(), "UTF-8") + "]");
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("��Ϣ���ͳɹ�! " + token.getResponse().getKey());
                }

				 
            });
            
            


            
            
            sampleClient.subscribe(Config.waitTopic);
            System.out.println("����"+Config.waitTopic+"�ɹ�!");
            
            String content = "{\"fds\":\"rtgeg\"}";
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(1);
            sampleClient.publish(topic, message);
            System.out.println("��Ϣ�����ɹ�!");
            

    		

        } catch (MqttException me) {
            System.out.println("MQTT�����쳣: �쳣�� " + me.getReasonCode() + ". ��Ϣ:" + me.getMessage() + " . ����ԭ��:" + me.getCause() + ". �쳣:"
                    + me);
        }
    }

    

    */
/**
     * �û������ù���: ToUpperCase(MD5_32(productKey+productSecret+deviceId+deviceSecret))
     * @param deviceId 
     *
     * @return
     *//*

    private static String signUserName(String deviceId) {
        String sign = Config.productKey + Config.productSecret + deviceId + Config.deviceSecret;
        String signUserName = Md5.getInstance().md5_32(sign).toUpperCase();
        return signUserName;
    }

    private static String signUserName4Device2(String deviceId) {
    	String sign = Config.productKey + Config.productSecret + deviceId + Config.deviceSecret2;
        String signUserName = Md5.getInstance().md5_32(sign).toUpperCase();
        return signUserName;
    }

    private static SSLSocketFactory createSSLSocket(byte[] pubKeyByteContent) throws Exception {
        InputStream is = new ByteArrayInputStream(pubKeyByteContent);
        InputStream caInput = new BufferedInputStream(is);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate ca = null;
        try {
            ca = cf.generateCertificate(caInput);
        } catch (CertificateException e) {
            e.printStackTrace();
        } finally {
            caInput.close();
        }
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
        SSLContext context = SSLContext.getInstance("TLSV1.2");
        context.init(null, tmf.getTrustManagers(), null);
        SSLSocketFactory socketFactory = context.getSocketFactory();
        return socketFactory;
    }

}
*/
